﻿using System.Collections.Generic;
using System.Linq;
using Gp.Scripts.Core;
using Sirenix.OdinInspector;

namespace Gp.Scripts.Core {
    public enum MagicField {
        [LabelText("行动点消耗")] ActionCost,


        [LabelText("法力点消耗")] MagicCost,


        [LabelText("法术伤害")] MagicDamage,


        [LabelText("持续时间")] Duration,
    }



    public class MagicBonusContainer {
        private struct MagicBonusItem {
            public readonly IBonusProvider Provider;
            public readonly ModifierValue Modifier;
            public readonly MagicField MagicField;
            public readonly bool IsPersistent;
            public MagicBonusItem(IBonusProvider provider, ModifierValue modifier, MagicField field) {
                Provider = provider;
                Modifier = modifier;
                MagicField = field;
                IsPersistent = true;
            }

            /// <summary>
            /// 
            /// </summary>
            /// <param name="provider"></param>
            /// <param name="modifier"></param>
            /// <param name="magicField"></param>
            /// <param name="isPersistent">如果为false, 则会在下一次使用法术后清除</param>
            public MagicBonusItem(IBonusProvider provider, ModifierValue modifier, MagicField magicField, bool isPersistent) {
                Provider = provider;
                Modifier = modifier;
                MagicField = magicField;
                IsPersistent = isPersistent;
            }
        }

        
        private List<MagicBonusItem> _list = new();
        
        
        public ModifierValue GetBonusByType(MagicField type) {
            ModifierValue res = new();
            
            foreach (var i in _list) {
                if (i.MagicField != type) continue;
                res += i.Modifier;
            }

            return res;
        }


        public void RemoveBonus(IBonusProvider provider) {
            _list.RemoveAll(i => i.Provider == provider);
        }

        public void AddBonus(IBonusProvider provider, ModifierValue modifier, MagicField field, bool isPersistent = false) {
            _list.Add(new MagicBonusItem(provider, modifier, field, isPersistent));
        }


        public void AfterMagicCast() {
            _list.RemoveAll(i => !i.IsPersistent);
        }
        
    }
    
    
    
}